package com.mnsq.permission.service.impl;

import com.mnsq.permission.bean.condition.MenuCondition;
import com.mnsq.permission.bean.entity.Menus;
import com.mnsq.permission.bean.entity.RoleMenus;
import com.mnsq.permission.bean.entity.UserMenus;
import com.mnsq.permission.bean.model.MenuModel;
import com.mnsq.permission.dao.MenuRepository;
import com.mnsq.permission.dao.RoleMenusRepository;
import com.mnsq.permission.dao.UserMenusRepository;
import com.mnsq.permission.service.MenuService;
import com.mnsq.user.bean.entity.User;
import com.mnsq.user.dao.UserRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 周俭
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleMenusRepository roleMenusRepository;
    @Autowired
    private UserMenusRepository userMenusRepository;
    @Autowired
    private MenuRepository menuRepository;

    @Override
//    @AutoCache(cls = List.class, properties = "userUnique", cacheType = AutoCache.CacheType.HOURS, value = 2)
    public List<MenuModel> queryListByCondition(MenuCondition menuCondition) {
        User user = userRepository.findUserByUnique(menuCondition.getUnique());
        if (user == null) {
            return null;
        }
        List<Menus> listMenus = new ArrayList<>();
        Long roleId = user.getRoleId();
        List<RoleMenus> roleMenus = null;
        if (CollectionUtils.isNotEmpty(roleMenus = roleMenusRepository.findAllByRoleId(roleId))) {
            List<Long> ids = roleMenus.stream().filter(p -> p.getStatus() == 1).map(p -> p.getMenuId()).collect(Collectors.toList());
            listMenus.addAll(menuRepository.findAllByIdIn(ids));
        }
        Long userId = user.getId();
        List<UserMenus> temp = null;
        if (CollectionUtils.isNotEmpty(temp = userMenusRepository.findAllByUserId(userId))) {
            List<Long> ids = temp.stream().map(p -> p.getMenuId()).collect(Collectors.toList());
            listMenus.addAll(menuRepository.findAllByIdIn(ids));
        }
        listMenus = listMenus.stream().filter(p -> p.getStatus() == 1).collect(Collectors.toList());
        return packageList(listMenus);
    }

    private List<MenuModel> packageList(List<Menus> listMenus) {
        List<MenuModel> resList = new ArrayList<>();
        List<MenuModel> secondList = new ArrayList<>();
        Iterator<Menus> iterator = listMenus.iterator();
        while (iterator.hasNext()) {
            Menus menus = iterator.next();
            if (menus.getLevel() == 1) {
                MenuModel temp = new MenuModel()
                        .setName(menus.getTitle())
                        .setLevel(1)
                        .setIcon(menus.getIcon())
                        .setUrl(menus.getUrl())
                        .setType(0);
                temp.setId(menus.getId());
                resList.add(temp);
            } else {
                MenuModel temp = new MenuModel()
                        .setName(menus.getTitle())
                        .setParentId(menus.getParentId())
                        .setLevel(2)
                        .setIcon(menus.getIcon())
                        .setUrl(menus.getUrl())
                        .setType(1);
                temp.setId(menus.getId());
                secondList.add(temp);
            }
        }
        for (MenuModel mm : secondList) {
            for (MenuModel rs : resList) {
                if (rs.getId().equals(mm.getParentId())) {
                    rs.getChildren().add(mm);
                    break;
                }
            }
        }
        return resList;
    }
}
